package org.moyakarta.server;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

import javax.servlet.GenericServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class KmlServlet extends GenericServlet
{

   private static final int BUFSIZE = 1024;

   @Override
   public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
   {
      System.out.println(">>> alexey: KmlServlet.service ===================== ");
      String filename = "";
      File f = null;
      FileInputStream fis = null;

      String url = req.getParameter("url");
      System.out.println(">>> alexey: KmlServlet.service url = " + url);
      if (url != null)
      {
         // new URL(url).
         req.getRequestDispatcher(url).include(req, res);
      }
      else
      {
         try
         {
            filename = "cta2.kml";
            System.out.println(">>> alexey: KmlServlet.service filename = " + filename);
            System.out.println(">>> alexey: KmlServlet.service getServletContext().getRealPath(filename) = "
               + getServletContext().getRealPath(filename));
            filename = getServletContext().getRealPath(filename);
            f = new File(filename);
            fis = new FileInputStream(f);
         }
         catch (Exception e)
         {
            e.printStackTrace();
         }

         doDownload((HttpServletRequest)req, (HttpServletResponse)res, filename, "cta2.kml");
      }  
   }

   /**
    *  Sends a file to the ServletResponse output stream.  Typically
    *  you want the browser to receive a different name than the
    *  name the file has been saved in your local database, since
    *  your local names need to be unique.
    *
    *  @param req The request
    *  @param res The response
    *  @param filename The name of the file you want to download.
    *  @param original_filename The name the browser should receive.
    */
   private void doDownload(HttpServletRequest req, HttpServletResponse res, String filename, String original_filename)
      throws IOException
   {
      File f = new File(filename);
      int length = 0;
      ServletOutputStream op = res.getOutputStream();
      ServletContext context = getServletConfig().getServletContext();
      String mimetype = context.getMimeType(filename);
      System.out.println(">>> alexey: KmlServlet.doDownload mimetype = " + mimetype);

      //
      //  Set the response and go!
      //
      res.setContentType((mimetype != null) ? mimetype : "application/octet-stream");
      res.setContentLength((int)f.length());
      res.setHeader("Content-Disposition", "attachment; filename=\"" + original_filename + "\"");

      //
      //  Stream to the requester.
      //
      byte[] bbuf = new byte[BUFSIZE];
      DataInputStream in = new DataInputStream(new FileInputStream(f));

      while ((in != null) && ((length = in.read(bbuf)) != -1))
      {
         op.write(bbuf, 0, length);
      }

      in.close();
      op.flush();
      op.close();
   }

}
